/**
 * @created by zhangpeng18 on 2019-8-5 10:40:22
 * @updated by
 * @description Java类驱动管理
 */
define(['./custom/widgets/js/yufpSelectParam.js'], function (require, exports) {
    /**
     * 页面加载完成时触发
     * @param hashCode 路由ID
     * @param data 传递数据对象
     * @param cite 页面站点信息
     */
    exports.ready = function (hashCode, data, cite) {
        yufp.lookup.reg('PROXY_FLAG');
        yufp.custom.vue({
            el: cite.el,
            data: function () {

                var _this = this;
                /**
                 * 校验自定义参数代码字段，必须以 $ 开头
                 */
                var paramCodeValidate = function (rule, value, callback) {
                    var reg = /^\$/;
                    if (value && reg.test(value)) {
                        callback();
                    } else if (value && !reg.test(value)) {
                        callback(new Error('自定义参数代码字段，请以 $ 开头'));
                    } else if (value === '') {
                        callback(new Error('自定义参数代码字段，请以 $ 开头'));
                    } else {
                        callback();
                    }
                };
                return {
                    searchFormdata: {},
                    addFnButton: !yufp.session.checkCtrl('addFn'),
                    updateFnButton: !yufp.session.checkCtrl('updateFn'),
                    deleteFnButton: !yufp.session.checkCtrl('deleteFn'),
                    treeDataUrl: "api/use/plats/apps/getTreeWftList",
                    dataUrl: 'api/use/plats/jccfg/queryList',
                    saveBtnShow: true,
                    cancelBtnShow: true,
                    addBtnShow: false,
                    saveDisabled: false,
                    formdata: {},
                    async: false,
                    param: { UNITID: '0000', LEVELUNIT: '1' },
                    rule: [
                        { validator: paramCodeValidate, trigger: 'blur' }
                    ],
                    height: yufp.frame.size().height,
                    dialogVisible: false,
                    formDisabled: false,
                    classCodeDisabled: false,
                    viewTitleArrry: ['Java类驱动新增', 'Java类驱动修改', 'Java类驱动查看'],
                    viewTitle: '',
                    propTabaleData: [],
                    appCodeShow: true,
                    propTableData: [],
                    nodeData: '', // 在新增表格数据时需要先获取当前树节点，再把一条新的记录加到这个节点下的表格里,
                    nodeLeaf: false,
                    isReadOnly: false,
                    agentIds: [
                    ],
                    nodeTemp: {},
                };
            },
            methods: {

                reloadData: function () {
                    var _this = this;
                    var paramObj = _this.$refs.queryModelRef.fm;
                    if (_this.nodeTemp.appCode != null && _this.nodeTemp.appCode != '0000') {
                        paramObj.appCode = _this.nodeTemp.appCode;
                    }
                    _this.$refs.refTable.remoteData(paramObj);
                },

                /**
                 * 获取参数列表子组件选中的行数据
                 */
                getSelectedData: function (data) {
                    var _this = this;
                    _this.$refs.refPropTable.selections[0].paramValue = '{#' + data[0].paramValue + '#}';
                },
                /**
                 * 取消
                 */
                cancelFn: function () {
                    var _this = this;
                    _this.dialogVisible = false;
                },
                /**
                 * 保存
                 */
                saveFn: function () {
                    var _this = this;
                    var validate = false;
                    _this.$refs.refForm.validate(function (valid) {
                        validate = valid;
                    });
                    if (!validate) {
                        return;
                    }

                    _this.checkClassCode(validate);

                    if (!validate) {
                        return;
                    }

                    var paramObj = _this.formdata;


                    if (paramObj.useParamValues != null) {
                        paramObj.useParamValues.splice(0);
                    } else {
                        paramObj.useParamValues = new Array();
                    }

                    for (let i = 0; i < _this.propTableData.length; i++) {
                        paramObj.useParamValues.push(_this.propTableData[i]);
                    }

                    let urlparam = "";
                    if (_this.addFlag) {
                        paramObj.appCode = _this.nodeTemp.appCode;
                        urlparam = 'api/use/plats/jccfg/saveUseJcCfgPo';
                    } else {
                        urlparam = 'api/use/plats/jccfg/updateUseJcCfgPo';
                    }


                    yufp.service.request({
                        method: 'POST',
                        url: urlparam,
                        data: paramObj,
                        callback: function (code, message, response) {

                            if (response.code == 0) {
                                _this.reloadData();

                                _this.$message('操作成功');
                                _this.dialogVisible = false;
                            } else {
                                _this.$message({ message: response.message, type: "warning" });
                            }
                        }
                    });

                },
                /**
                 * 控制保存按钮、xdialog、表单的状态
                 * @param viewType 表单类型
                 * @param editable 可编辑,默认false
                 */
                switchStatus: function (viewType, editable) {
                    var _this = this;
                    _this.viewType = viewType;
                    _this.dialogVisible = true;
                    _this.formDisabled = !editable;
                    _this.saveBtnShow = editable;
                    _this.cancelBtnShow = editable;
                },
                /**
                 * 新增参数
                 */
                propAddFn: function () {
                    var _this = this;
                    var obj = {
                        paramCode: '',
                        paramName: '',
                        paramValue: ''
                    };
                    _this.propTableData.push(obj);
                },
                /**
                 * 删除参数
                 */
                propDeleteFN: function () {
                    var _this = this;

                    let selectDatas = _this.$refs.refPropTable.selections;
                    for (let jdx = _this.propTableData.length - 1; jdx >= 0; jdx--) {
                        for (let idx = 0; selectDatas.length > idx; idx++) {
                            if (_this.propTableData[jdx] == selectDatas[idx]) {
                                _this.propTableData.splice(jdx, 1);
                                break;
                            }
                        }

                    }
                },
                /**
                 * 点击树的结点，加载对应的数据
                 */
                nodeClickFn: function (nodeData, node, self) {
                    var _this = this;
                    var appCode = "";
                    if (nodeData.appCode != '0000') {
                        _this.nodeLeaf = true;
                        appCode = nodeData.appCode;
                    } else {
                        _this.nodeLeaf = false;
                    }
                    _this.nodeTemp.appCode = nodeData.appCode;
                    _this.$refs.refTable.remoteData({ appCode: appCode });
                },
                /**
                 * 新增
                 */
                addFn: function () {
                    var _this = this;
                    _this.isReadOnly = false;
                    _this.dialogVisible = true;
                    _this.viewTitle = _this.viewTitleArrry[0];
                    _this.addFlag = true;
                    _this.saveDisabled = false;
                    _this.getAgentIds(_this.nodeTemp.appCode);

                    _this.$nextTick(function () {
                        _this.$refs.refForm.resetFields();
                        _this.propTableData = [];
                        // 新增表格的数据时需要手动赋一些值
                        _this.formdata.id = null;
                        _this.formdata.driverId = _this.nodeData.id;
                        _this.formdata.appCode = _this.nodeData.appCode;
                        _this.formdata.appName = _this.nodeData.appName;
                        _this.formdata.errorReturn = '2';
                    });
                },
                /**
                 * 修改
                 */
                modifyFn: function () {
                    var _this = this;
                    if (_this.$refs.refTable.selections.length != 1) {
                        _this.$message({ message: '请先选择一条记录', type: 'warning', duration: 6000 });
                        return;
                    }
                    _this.isReadOnly = true;
                    _this.nodeTemp.appCode = _this.$refs.refTable.selections[0].appCode;
                    _this.getAgentIds(_this.$refs.refTable.selections[0].appCode);
                    _this.viewTitle = _this.viewTitleArrry[1];
                    _this.saveDisabled = false;
                    _this.dialogVisible = true;
                    _this.addFlag = false;
                    _this.refTemp = _this.$refs.refTable.selections[0];
                    var data = _this.$refs.refTable.selections[0].useParamValues;
                    _this.$nextTick(function () {
                        _this.$refs.refForm.resetFields();
                        _this.propTableData.splice(0);
                        yufp.clone(_this.refTemp, _this.formdata);
                        // _this.$refs.refPropTable.remoteData(propTableParams);
                        if (data != null)
                            for (var i = 0; i < data.length; i++) {
                                _this.propTableData.push(data[i]);
                            }
                    });
                },

                viewFn: function (obj) {
                    var _this = this;

                    _this.isReadOnly = true;
                    _this.nodeTemp.appCode = obj.appCode;
                    _this.getAgentIds(obj.appCode);
                    _this.viewTitle = _this.viewTitleArrry[2];
                    _this.dialogVisible = true;
                    _this.saveDisabled = true;
                    _this.addFlag = false;
                    _this.refTemp = obj;
                    var data = obj.useParamValues;
                    _this.$nextTick(function () {
                        _this.$refs.refForm.resetFields();
                        _this.propTableData.splice(0);
                        yufp.clone(_this.refTemp, _this.formdata);
                        // _this.$refs.refPropTable.remoteData(propTableParams);
                        if (data != null)
                            for (var i = 0; i < data.length; i++) {
                                _this.propTableData.push(data[i]);
                            }
                    });

                },


                /**
                 * 删除
                 */
                deleteFn: function () {
                    var _this = this;
                    var selections = _this.$refs.refTable.selections;
                    if (selections.length < 1) {
                        _this.$message({ message: '请先至少选择一条记录', type: 'warning', duration: 6000 });
                        return;
                    }
                    var len = selections.length, arr = [];
                    for (var i = 0; i < len; i++) {
                        arr.push(selections[i].id);
                    }
                    _this.$confirm('此操作将永久删除该记录, 是否继续?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning', duration: 6000,
                        center: true,
                        callback: function (action) {
                            if (action === 'confirm') {
                                yufp.service.request({
                                    method: 'DELETE',
                                    url: "api/use/plats/jccfg/deleteUseJcCfgPo",
                                    data: selections[0],
                                    callback: function (code, message, response) {
                                        if (response.code != 0) {
                                            isOk = false;
                                            _this.$message(response.message);
                                        } else {
                                            _this.$message('操作成功');
                                            _this.reloadData();

                                        }

                                    }
                                });
                            }
                        }
                    });
                },
                getAgentIds: function (appCode) {

                    var _this = this;
                    _this.agentIds.splice(0);
                    const model = {};
                    model.appCode = appCode;

                    var params = { condition: JSON.stringify(model) };

                    var initObj = {};
                    initObj.key = "_0";
                    initObj.value = "虚拟代理";
                    _this.agentIds.push(initObj);
                    yufp.service.request({
                        method: 'GET',
                        url: "api/use/plats/apps/queryWftAppList",
                        data: params,
                        async: false,
                        callback: function (code, message, response) {

                            if (response.data.length > 0) {
                                const wftAgtList = response.data[0].wftAgtList;

                                if (wftAgtList != null)
                                    for (let j = 0; j < wftAgtList.length; j++) {
                                        var obj = {};
                                        obj.key = wftAgtList[j].agentId;
                                        obj.value = wftAgtList[j].agentName;
                                        _this.agentIds.push(obj);

                                    }
                            }
                        }
                    });

                },
                checkClassCode: function (isOk) {

                    var _this = this;
                    var paramObj = _this.formdata;
                    paramObj.useParamValues = [];
                    if (!_this.addFlag) {
                        return;
                    }

                    yufp.service.request({
                        method: 'GET',
                        url: "api/use/plats/dscfg/checkClassCode",
                        data: paramObj,
                        async: false,
                        callback: function (code, message, response) {
                            if (response.code != 0) {
                                isOk = false;
                                _this.$message(response.message);
                            } else {
                                if (!response.data) {
                                    isOk = false;
                                    _this.$message('该驱动缩写在系统中已经存在');
                                }
                            }

                        }
                    });
                },
            },

        });
    };
});